<?php

namespace App\Kuafu\User\Controller\Internal;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Common\Enum\CodeEnum;
use App\Kuafu\User\Model\Permissions;
use App\Kuafu\User\Service\AuthService;
use App\Kuafu\User\Service\PermissionsService;
use App\Kuafu\User\Service\UserRoleService;
use Illuminate\Http\Request;

class PermissionsController extends BaseController
{
    /**
     * 获取用户所有的权限 列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getUserAllPermission(Request $request)
    {
//        $userId = $request->userId;
//
//        // 获取当前用户的角色 ID
//        $roleList = UserRoleService::getWebUserRoleList((int)$userId, (int)$brandId, (int)$shopId);
//
//        $permission = new PermissionsService($request->header('brandId'), $request->header('shopId'), $roleId,
//            CodeEnum::PLATFORM_B_BRAND);
//        $return     = $permission->getPermissionsList();
//
//        return $this->success($return);
    }

    public function getAllPermission(Request $request)
    {
        $platform = $request->get('platform', CodeEnum::PLATFORM_B_BRAND);
        $module   = strtolower($request->get('module', 'all'));

        $permissionAll = Permissions::getAll($platform)->pluck('action')->map(function ($value) {
            return strtolower($value);
        });

        $permissionList = [];

        if ($module != 'all') {
            $permissionAll->each(function ($v, $k) use ($module, &$permissionList) {
                switch ($module) {
                    case 'openshop':
                    default:
                        $perStr = explode('.', $v);
                        foreach ($perStr as $str) {
                            if ($str == $module) {
                                $permissionList[] = $v;
                                break;
                            }
                        }
                }
            });
        } else {
            $permissionList = $permissionAll->toArray();
        }

        return $this->success($permissionList);
    }
}
